package br.com.sbc.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.sbc.ejb.entity.Usuario;


@WebFilter(urlPatterns="/*", filterName="MenuFilter")
public class MenuFilter implements Filter{
	
	private static final String USUARIOCOMUM = "usuarioComum";
	private static final String ADMINISTRADOR = "administrador";
	private static final String PAGINA_LOGIN = "/dinamic/pages/principal/login.jsf";
	private static final String PAGINA_HOME = "/dinamic/pages/principal/home.jsf";
	private static Usuario usuario;

    public MenuFilter() {

    }

	public void destroy() {
		
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest req = (HttpServletRequest) request;
		HttpSession session = req.getSession();
		usuario = (Usuario) session.getAttribute("usuario");
		
		HttpServletResponse resp = (HttpServletResponse) response;

		if(provedorPaginasLiberadas(req) || usuario != null){
			String perfil = recuperaPerfilUsuario(session);
			if (perfil != "") {
				if (ADMINISTRADOR.equals(perfil.toString())) {
					if (isUrlRestritaAoAdministrador(req)) {
						resp.sendRedirect(req.getContextPath() + PAGINA_LOGIN);
					} else {
						chain.doFilter(request, response);
					}	
				}else if(USUARIOCOMUM.equals(perfil.toString())){
					if (isUrlRestritaAoUsuarioComum(req)) {
						resp.sendRedirect(req.getContextPath() + PAGINA_LOGIN);
					} else {
						chain.doFilter(request, response);
					}
				}
			} else {
				chain.doFilter(request, response);
			}
		} else {
			resp.sendRedirect(req.getContextPath() + PAGINA_HOME);
		}
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}
	
	private boolean isUrlRestritaAoAdministrador(HttpServletRequest request){
		return false;
	}
	
	private boolean isUrlRestritaAoUsuarioComum(HttpServletRequest req){
		return (
				req.getRequestURI().contains("administracao"));
	}
	
	private String recuperaPerfilUsuario(HttpSession session){
		usuario = (Usuario) session.getAttribute("usuario");
		if (usuario != null) {
			return usuario.getPerfil().toString();
		}
		return "";
	}
	
	private boolean provedorPaginasLiberadas(HttpServletRequest req){
		return (req.getRequestURI().endsWith("login.jsf") || req.getRequestURI().endsWith("home.jsf")
				|| req.getRequestURI().endsWith("index.jsf")
				|| req.getRequestURI().endsWith("baseDeConhecimento.jsf")
				|| req.getRequestURI().endsWith("teste.jsf")
				|| req.getRequestURI().endsWith("incluirPostagem.jsf")
				|| req.getRequestURI().endsWith("registro.jsf")
				|| req.getRequestURI().endsWith("listaPostagem.jsf")
				|| req.getRequestURI().endsWith("resultadoPesquisa.jsf")
				|| req.getRequestURI().endsWith("detalhaPostagem.jsf")
				|| req.getRequestURI().endsWith("postagensDoAssunto.jsf")
				|| req.getRequestURI().contains("javax.faces.resource")
				|| req.getRequestURI().contains("static")
				|| req.getRequestURI().contains("fotoPerfil"));
		
	}

}
